package com.exam.api.modules.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exam.api.common.web.TinyResponse;
import com.exam.api.modules.dict.dto.*;
import com.exam.api.modules.dict.service.SysDictItemService;
import com.exam.api.modules.dict.service.SysDictService;
import com.exam.api.modules.dict.vo.SysDictItemVO;
import com.exam.api.modules.dict.vo.SysDictVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * (管理端) 字典相关接口
 *
 * @author iaa
 */
@Tag(name = "(管理端) 字典相关接口", description = "(管理端) 字典相关接口")
@RestController
@RequestMapping("/manager/dict")
@TinyResponse
public class DictManagerController {

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysDictItemService sysDictItemService;

    @Parameter(name = "code", description = "", in = ParameterIn.PATH, required = true)
    @Operation(summary = "根据字典编码查询字典项", description = "")
    @PostMapping("/code/{code}")
    public List<DictOptionDTO> getDictItemByDictCode(@PathVariable("code") String code) {
        return sysDictItemService.getDictItemByDictCode(code);
    }

    @Operation(summary = "根据字典编码数组查询字典项", description = "")
    @PostMapping("/codes")
    public Map<String, List<DictOptionDTO>> getDictItemByCodeList(@RequestBody DictCodeDTO dto) {
        return sysDictItemService.getDictItemByCodeList(dto.getCodes());
    }

    @Operation(summary = "创建字典", description = "")
    @PostMapping("/create")
    public void createSysDict(@RequestBody SysDictDTO dto) {
        sysDictService.createSysDict(dto);
    }


    @Operation(summary = "修改字典", description = "")
    @PostMapping("/update")
    public void updateSysDict(@RequestBody SysDictDTO dto) {
        sysDictService.updateSysDict(dto);
    }

    // 分页查询
    @Operation(summary = "分页查询字典", description = "分页查询")
    @PostMapping("/page")
    public IPage<SysDictVO> pageSysDict(@RequestBody SysDictSearchDTO dto) {
        return sysDictService.pageSysDict(dto);
    }

    @Parameter(name = "id", description = "", in = ParameterIn.PATH, required = true)
    @Operation(summary = "id查询字典", description = "id查询字典")
    @PostMapping("/{dictId}")
    public SysDictVO getSysDictById(@PathVariable("dictId") Long id) {
        return sysDictService.selectById(id);
    }

    @Parameter(name = "dictId", description = "", in = ParameterIn.PATH, required = true)
    @Operation(summary = "删除字典", description = "删除字典")
    @PostMapping("/delete/{dictId}")
    public void deleteSysDict(@PathVariable("dictId") Long dictId) {
        sysDictService.delSysDict(dictId);
    }

    @Operation(summary = "查询字典项", description = "查询字典项")
    @PostMapping("/item/page")
    public IPage<SysDictItemVO> selectDictItemPage(@RequestBody SysDictItemSearchDTO dto) {
        return sysDictItemService.pageSysDictItem(dto);
    }

    @Operation(summary = "创建字典项", description = "创建字典项")
    @PostMapping("/item/create")
    public void createSysDictItem(@RequestBody SysDictItemDTO dto) {
        sysDictItemService.createSysDictItem(dto);
    }

    @Operation(summary = "修改字典", description = "修改字典")
    @PostMapping("/item/update")
    public void updateSysDictItem(@RequestBody SysDictItemDTO dto) {
        sysDictItemService.updateSysDictItem(dto);
    }

    @Parameter(name = "dictItemId", description = "", in = ParameterIn.PATH, required = true)
    @Operation(summary = "dictItemId查询字典", description = "dictItemId查询字典")
    @PostMapping("/item/{dictItemId}")
    public SysDictItemVO getSysDictItemById(@PathVariable("dictItemId") Long dictItemId) {
        return sysDictItemService.selectById(dictItemId);
    }

    @Parameter(name = "dictItemId", description = "", in = ParameterIn.PATH, required = true)
    @Operation(summary = "删除字典", description = "")
    @PostMapping("/item/delete/{dictItemId}")
    public void deleteSysDictItem(@PathVariable("dictItemId") Long dictItemId) {
        sysDictItemService.delSysDictItem(dictItemId);
    }
}
